/*
 * Project Name: showtao
 * File Name: ProductRepository.java
 * Interface Name: ProductRepository
 *
 * Copyright 2014 showtao.com
 *
 * Licensed under the showtao.com
 *
 * http://showtao.com
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.showtao.front.repository;

import java.util.Set;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.showtao.front.domain.Product;
import com.showtao.front.domain.ProductCategory;
import com.showtao.front.domain.VehicleModel;

public interface ProductRepository extends JpaRepository<Product, Long>,
		JpaSpecificationExecutor<Product> {
    
    Set<Product> findByModelAndCategory(VehicleModel model, ProductCategory category);
    
    @Query("select p from Product p "
            + " left join p.category "
    		+ " left join p.introPic "
    		+ " left join p.paramPic "
    		+ " left join p.dismountingPic "
    		+ " left join p.mergingPic "
            + " left join p.model m " 
            + "   left join m.line l " 
            + "     left join l.brand")
    Page<Product> findAllJoinFully(Pageable pageable);
    
    @Query("select p from Product p "
    		+ " left join p.category "
    		+ " where p.id = :id")
    Product getOneJoinCategory(@Param("id")Long id);
    
    @Query("select p from Product p "
            + " left join p.category "
    		+ " left join p.introPic "
    		+ " left join p.paramPic "
    		+ " left join p.dismountingPic "
    		+ " left join p.mergingPic "
            + " left join p.model m " 
            + "   left join m.line l " 
            + "     left join l.brand " 
            + " where p.id = :id")
    Product getOneJoinFully(@Param("id")Long id);
    
    @Query("select p from Product p "
            + " left join p.category "
    		+ " left join p.introPic "
    		+ " left join p.paramPic "
    		+ " left join p.dismountingPic "
    		+ " left join p.mergingPic "
            + " left join p.model m " 
            + "   left join m.line l " 
            + "     left join l.brand " 
            + " where p.code = :code")
    Product getProductByCode(@Param("code")String code);
    
    @Query("select p from Product p "
    		+ " left join p.category c "
    		+ " left join p.model m "
    		+ 	" left join m.line "
    		+ " where c.code = :category and p.code like :code")
    Page<Product> findByCategoryAndCodeLike(@Param("category") String category, @Param("code") String code, Pageable pageble);
}